import 'package:flutter/material.dart';
import '../../models/home_model.dart';
import '../../components/dashed_line.dart';
import '../../components/star_rating.dart';

class MovieListItem extends StatelessWidget {
  final MovieItem movie;

  MovieListItem(this.movie);

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.all(10),
      decoration: BoxDecoration(
        border: Border(bottom: BorderSide(width: 10, color: Color(0xffe2e2e2))),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          _getMovieRankWidget(),
          SizedBox(height: 12),
          _getMovieContentWidget(),
          SizedBox(height: 12),
          _getMovieIntroduceWidget(),
          SizedBox(height: 12)
        ],
      ),
    );
  }

  /// 获取电影排名
  Widget _getMovieRankWidget() {
    return Container(
      padding: EdgeInsets.fromLTRB(9, 4, 9, 4),
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(3),
        color: Color.fromARGB(255, 238, 205, 144),
      ),
      child: Text(
        'No.${movie.rank}',
        style: TextStyle(fontSize: 18, color: Color.fromARGB(255, 131, 95, 36)),
      ),
    );
  }

  /// 获取具体内容
  Widget _getMovieContentWidget() {
    return Container(
      height: 150,
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          _getContentImage(),
          _getContentDesc(),
          _getDashedLine(),
          _getContentWish(),
        ],
      ),
    );
  }

  Widget _getDashedLine() {
    return Container(
      width: 1,
      height: 100,
      child: DashedLine(
        axis: Axis.vertical,
        dashedHeight: 6,
        dashedWidth: .5,
        count: 12,
      ),
    );
  }

  /// 获取图片海报
  Widget _getContentImage() {
    return ClipRRect(
      borderRadius: BorderRadius.circular(5),
      child: Image.network(movie.imageURL),
    );
  }

  Widget _getContentDesc() {
    return Expanded(
      child: Container(
        padding: EdgeInsets.symmetric(horizontal: 15),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            _getTitleWidget(),
            SizedBox(height: 3),
            _getRatingWidget(),
            SizedBox(height: 3),
            _getInfoWidget(),
          ],
        ),
      ),
    );
  }

  Widget _getTitleWidget() {
    return Stack(
      children: [
        Icon(Icons.play_circle_outline, color: Colors.redAccent),
        Text.rich(
          TextSpan(children: [
            TextSpan(
              text: '     ' + movie.title,
              style: TextStyle(
                fontSize: 18,
                fontWeight: FontWeight.bold,
              ),
            ),
            TextSpan(
              text: '(${movie.playDate})',
              style: TextStyle(
                fontSize: 16,
                color: Colors.black54,
              ),
            ),
          ]),
          maxLines: 2,
        ),
      ],
    );
  }

  Widget _getRatingWidget() {
    return Row(
      crossAxisAlignment: CrossAxisAlignment.end,
      children: [
        TTStarRating(rating: movie.rating, size: 18),
        SizedBox(width: 5),
        Text('${movie.rating}'),
      ],
    );
  }

  Widget _getInfoWidget() {
    // 获取种类字符串
    final genres = movie.genres.join(' ');
    final director = movie.director.name;
    var castString = '';
    for (var cast in movie.casts) {
      castString += cast.name + ' ';
    }

    return Text(
      '$genres / $director / $castString',
      maxLines: 2,
      overflow: TextOverflow.ellipsis,
      style: TextStyle(fontSize: 16),
    );
  }

  Widget _getContentWish() {
    return Container(
      width: 60,
      child: Column(
        children: [
          SizedBox(height: 20),
          Image.asset('assets/images/home/wish.png', width: 30),
          SizedBox(height: 5),
          Text(
            '想看',
            style: TextStyle(
              fontSize: 16,
              color: Color.fromARGB(255, 235, 170, 60),
            ),
          ),
        ],
      ),
    );
  }

  /// 电影简介
  Widget _getMovieIntroduceWidget() {
    return Container(
      width: double.infinity,
      padding: EdgeInsets.all(12),
      decoration: BoxDecoration(
        color: Color(0xfff2f2f2),
        borderRadius: BorderRadius.circular(5),
      ),
      child: Text(
        movie.originalTitle,
        style: TextStyle(fontSize: 18, color: Colors.black54),
      ),
    );
  }
}
